package cn.lg.soar.system.biz.modules.auth.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.database.util.MpSqlUtils;
import cn.lg.soar.database.util.PageUtils;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.common.enums.LogTypeEnum;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.biz.modules.auth.entity.Permit;
import cn.lg.soar.system.biz.modules.auth.service.IPermitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Tag(name = "权限管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/permit")
public class PermitController extends BaseController<Permit, IPermitService> {

    @Override
    protected Serializable getId(Permit entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "权限";

    @DisabledTenantAccess
    @Log(value = "新增" + SUBJECT, type = LogTypeEnum.CREATE)
    @IdempotentByParam("#p0")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated Permit permit) {
        return super.createResult(permit);
    }

    @DisabledTenantAccess
    @Log(value = "修改" + SUBJECT, type = LogTypeEnum.UPDATE)
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated Permit permit) {
        return super.updateResult(permit);
    }

    @DisabledTenantAccess
    @Log(value = "删除" + SUBJECT, type = LogTypeEnum.REMOVE)
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @DisabledTenantAccess
    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<Permit> detail(@RequestParam(name = "id") @Id Long id) {
        return super.detailResult(id);
    }

    @DisabledTenantAccess
    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<Permit>> query(PageQuery pageQuery, QueryParams params, Long[] menuIds){
        QueryWrapper<Permit> wrapper = new SqueryWapper<>(Permit.class).listWrapper(params);
        MpSqlUtils.handleInOrEq(menuIds, (sql, value) -> {
            wrapper.exists("select * from lg_sys_menu_2_permit m where lg_sys_permit.id=m.permit_id and m.menu_id " + sql, value);
        });
        IPage<Permit> page = service.page(PageUtils.toPage(pageQuery), wrapper);
        return HttpResult.data(PageUtils.toPageResult(page));
    }

    @DisabledTenantAccess
    @ListMapping
    public HttpResult<List<Permit>> list(QueryParams params){
        return HttpResult.data(
                super.getList(params)
        );
    }

    @DisabledTenantAccess
    @ExistMapping
    public HttpResult<Boolean> exist(QueryParams params){
        return super.existResult(params);
    }

    @DisabledTenantAccess
    @Log(value = "导出" + SUBJECT, type = LogTypeEnum.EXPORT)
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(QueryParams params, HttpServletResponse resp){
        super.exportExcel(PermitController.SUBJECT, params, resp);
    }

    @DisabledTenantAccess
    @Log(value = "导入" + SUBJECT, type = LogTypeEnum.IMPORT)
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @DisabledTenantAccess
    @Operation(summary = "扫描生成权限")
    @PostMapping(value = "scan", name = "扫描生成权限")
    public HttpResult<Void> scan(){
        this.service.scanServletPathGenPermit();
        return HttpResult.success();
    }

    @DisabledTenantAccess
    @Operation(summary = "清空缓存")
    @PostMapping(value = "clear-cache", name = "清空缓存")
    public HttpResult<Void> clearCache(){
        this.service.clearCache();
        return HttpResult.success();
    }
}
